package com.xing.binbackend.topology.remote;

import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public class WebClientService {






    private final WebClient client;

    public WebClientService(WebClient.Builder webClientBuilder) {
        this.client = webClientBuilder.baseUrl("http://10.173.75.252:6379/openapi").build();
    }

    public void readFile(String type) {
        JsonNode response1 =
                client.get().uri(uriBuilder -> uriBuilder.path("/firewall/table").queryParam(
                        "type", type).build()).retrieve().bodyToMono(JsonNode.class).block();

        Mono<JsonNode> entityMono =
                client.get().uri("/persons/1").accept(MediaType.APPLICATION_JSON).exchangeToMono(response -> {
            if (response.statusCode().equals(HttpStatus.OK)) {
                return response.bodyToMono(JsonNode.class);
            } else {
                // Turn to error
                return response.bodyToMono(JsonNode.class);
            }
        });
    }

    public void demo() {
        WebClient webClient = WebClient.create("http://10.173.75.252:6379/openapi");

//      https://blog.csdn.net/zwjyyy1203/article/details/93226900
//      https://springdoc.cn/spring-5-webclient
        // /products/2/attributes/13
        webClient.get().uri(uriBuilder -> uriBuilder.path("/products/{id}/attributes/{attributeId"
                + "}").build(2, 13)).retrieve().bodyToMono(String.class).block();

        // /products/?name=AndroidPhone&color=black&deliveryDate=13%2F04%2F2019
        webClient.get().uri(uriBuilder -> uriBuilder.path("/products/").queryParam("name",
                "{title}").queryParam("color", "{authorId}").queryParam("deliveryDate", "{date}").build("AndroidPhone", "black", "13/04/2019")).retrieve().bodyToMono(String.class).block();

        // /products/?category=Phones&category=Tablets
        webClient.get().uri(uriBuilder -> uriBuilder.path("/products/").queryParam("category",
                "Phones", "Tablets").build()).retrieve().bodyToMono(String.class).block();

        // /products/?category=Phones,Tablets
        webClient.get().uri(uriBuilder -> uriBuilder.path("/products/").queryParam("category",
                String.join(",", "Phones", "Tablets")).build()).retrieve().bodyToMono(String.class).block();

        // /products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019"
        webClient.get().uri(uriBuilder -> uriBuilder.path("/products/").queryParam("name",
                "AndroidPhone").queryParam("color", "black").queryParam("deliveryDate",
                "13/04" + "/2019").build()).retrieve().bodyToMono(String.class).block();

        WebClient webClient2 = WebClient.builder().defaultHeader("User-Agent",
                "SelfDefineHeader").baseUrl("http://127.0.0" + ".1:8080").build();

    }
}
